package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.model.product.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.exc.ValueInstantiationException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author dyqstart
 * @create 2021-08-02-18:35
 * @since 1.0.0
 */
@Api(tags = "根据分类id获取平台属性")
@RequestMapping("/admin/product/")
@RestController
@Slf4j
public class BaseAttrInfoController {

    @Autowired
    BaseAttrInfoService baseAttrInfoService;

    @Autowired
    BaseAttrValueService baseAttrValueService;

    @ApiOperation(value = "根据分类id获取平台属性")
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result<List<BaseAttrInfo>> attrInfoList(
            @ApiParam(value = "一级分类id")
            @PathVariable Long category1Id,
            @ApiParam(value = "二级分类id")
            @PathVariable Long category2Id,
            @ApiParam(value = "三级分类id")
            @PathVariable Long category3Id) {

        List<BaseAttrInfo> baseAttrInfoList = baseAttrInfoService.attrInfoList(category1Id, category2Id, category3Id);

        return Result.ok(baseAttrInfoList);
    }


    /**
     * 新增修改二合一
     *
     * @param baseAttrInfo 表单提交的k=v&k=v映射到BaseAttrInfo\\\前端提交的json封装用@RequestBody
     * @return
     */
    @ApiOperation("保存平台属性名和值")
    @PostMapping("saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo) {
        log.info("前端提交的数据" + baseAttrInfo);
        baseAttrInfoService.saveOrUpdateInfoAndValue(baseAttrInfo);
//        baseAttrInfoService.saveAttrInfoAndValue(baseAttrInfo);
        return Result.ok();
    }


    //    @ApiOperation("根据平台属性id获取平台属性对应数据")
//    @GetMapping("getAttrValueList/{attrId}")
//    public Result<List<BaseAttrValue>> getAttrValueList(
//            @ApiParam(value = "平台属性id",required = true)
//            @PathVariable("attrId") Long attrId){
//        QueryWrapper<BaseAttrValue> wrapper = new QueryWrapper<>();
//        wrapper.eq("attr_id",attrId);
//
//        List<BaseAttrValue> attrValueList = baseAttrValueService.list(wrapper);
//        return Result.ok(attrValueList);
//    }
    @GetMapping("/getAttrValueList/{attrId}")
    public Result<List<BaseAttrValue>> getAttrValueList(@PathVariable("attrId") Long attrId) {

        QueryWrapper<BaseAttrValue> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_id", attrId);
        List<BaseAttrValue> list = baseAttrValueService.list(wrapper);

        return Result.ok(list);
    }

}
